﻿Public Class OrderedArticle
    Inherits Article

    Private _orderid, _ordertext As String
    Private _orderedamount, _rebate As Decimal
    Private _orderposition As Integer
    Private _orderprice As New EMoney(0)

    Public Sub New(art As Article)
        ArticleID = art.ArticleID
        ArticleDescription = art.ArticleDescription
        TaxClass = art.TaxClass
        ArticleWeight = art.ArticleWeight
        ArticleText = art.ArticleText
        ArticlePrice1 = art.ArticlePrice1
        ArticlePrice2 = art.ArticlePrice2
        ArticlePrice3 = art.ArticlePrice3
        ArticleUnit = art.ArticleUnit
        ArticleCategory = art.ArticleCategory
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim _art As OrderedArticle = obj
            If (_art.ArticleID = ArticleID) AndAlso (OrderArticleID.Equals(_art.OrderArticleID)) Then
                Return True
            End If
        Catch : End Try
        Return False
    End Function

    Public Property OrderArticleID As String
        Get
            Return _orderid
        End Get
        Set(value As String)
            _orderid = value
            NotifyPropertyChanged("OrderArticleID")
        End Set
    End Property

    Public Property OrderedAmount() As Decimal
        Get
            Return _orderedamount
        End Get
        Set(ByVal value As Decimal)
            _orderedamount = value
            NotifyPropertyChanged("OrderedAmount")
            NotifyPropertyChanged("AmountString")
            NotifyPropertyChanged("NetOrderValue")
            NotifyPropertyChanged("GrossOrderValue")
            NotifyPropertyChanged("NetValue")
            NotifyPropertyChanged("GrossValue")

        End Set
    End Property

    Public Property OrderRebate() As Decimal
        Get
            Return _rebate
        End Get
        Set(ByVal value As Decimal)
            _rebate = value
            NotifyPropertyChanged("OrderRebate")
            NotifyPropertyChanged("NetOrderValue")
            NotifyPropertyChanged("GrossOrderValue")
            NotifyPropertyChanged("NetValue")
            NotifyPropertyChanged("GrossValue")
            NotifyPropertyChanged("DiscountString")
        End Set
    End Property

    Public Property OrderPrice() As EMoney
        Get
            Return _orderprice
        End Get
        Set(ByVal value As EMoney)
            _orderprice = value
            NotifyPropertyChanged("OrderPrice")
            NotifyPropertyChanged("NetOrderValue")
            NotifyPropertyChanged("GrossOrderValue")
            NotifyPropertyChanged("NetValue")
            NotifyPropertyChanged("GrossValue")
        End Set
    End Property

    Public Property OrderPosition() As Integer
        Get
            Return _orderposition
        End Get
        Set(ByVal value As Integer)
            _orderposition = value
            NotifyPropertyChanged("OrderPosition")
        End Set
    End Property

    Public Property OrderText() As String
        Get
            Return _ordertext
        End Get
        Set(ByVal value As String)
            _ordertext = value
            NotifyPropertyChanged("OrderText")
        End Set
    End Property

    Public Property AmountString As String
        Get
            Try
                Return OrderedAmount.ToString(ArticleUnit.UnitFormat) + " " + ArticleUnit.ShortName
            Catch ex As Exception
                Return OrderedAmount.ToString("0.000")
            End Try
        End Get
        Set(value As String)
            ' Try
            OrderedAmount = CDec(value.Replace(ArticleUnit.ShortName, "").Trim)
            NotifyPropertyChanged("AmountString")
            'Catch : End Try
        End Set
    End Property

    Public Property DiscountString As String
        Get
            Return (OrderRebate).ToString("0.00") + " %"
        End Get
        Set(value As String)
            'Try
            OrderRebate = CDec(value.Replace("%", "").Trim)
            NotifyPropertyChanged("DiscountString")
            'Catch : End Try
        End Set
    End Property

    ''' <summary>
    ''' Returns price X amount without taxes
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property NetOrderValue As Money
        Get
            If OrderPrice.IsGross Then
                Return New EMoney(Math.Round((Math.Round(OrderPrice.Amount * OrderedAmount, 2, MidpointRounding.AwayFromZero) * ((100 - OrderRebate) / 100)) / ((TaxClass.TaxRate + 100) / 100), 2, MidpointRounding.AwayFromZero), False, OrderPrice.Currency)
            Else
                Return New EMoney(Math.Round((Math.Round(OrderPrice.Amount * OrderedAmount, 2, MidpointRounding.AwayFromZero) * ((100 - OrderRebate) / 100)), 2, MidpointRounding.AwayFromZero), False, OrderPrice.Currency)
            End If
        End Get
    End Property

    ''' <summary>
    ''' Returns price X amount with taxes
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property GrossOrderValue As Money
        Get
            If OrderPrice.IsGross Then
                Return New EMoney(Math.Round((Math.Round(OrderPrice.Amount * OrderedAmount, 2, MidpointRounding.AwayFromZero) * ((100 - OrderRebate) / 100)), 2, MidpointRounding.AwayFromZero), True, OrderPrice.Currency)
            Else
                Return New EMoney(Math.Round((Math.Round(OrderPrice.Amount * OrderedAmount, 2, MidpointRounding.AwayFromZero) * ((100 - OrderRebate) / 100)) * ((TaxClass.TaxRate + 100) / 100), 2, MidpointRounding.AwayFromZero), True, OrderPrice.Currency)
            End If
        End Get
    End Property

    ''' <summary>
    ''' Returns price without discount and without tax
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property NetValue As Money
        Get
            If OrderPrice.IsGross Then
                Return New EMoney(Math.Round((OrderPrice.Amount) / ((TaxClass.TaxRate + 100) / 100), 2, MidpointRounding.AwayFromZero), False, OrderPrice.Currency)
            Else
                Return New EMoney(Math.Round((OrderPrice.Amount), 2, MidpointRounding.AwayFromZero), False, OrderPrice.Currency)
            End If
        End Get
    End Property

    ''' <summary>
    ''' Returns price without discount and with tax
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property GrossValue As Money
        Get
            If OrderPrice.IsGross Then
                Return New EMoney(Math.Round((OrderPrice.Amount), 2, MidpointRounding.AwayFromZero), True, OrderPrice.Currency)
            Else
                Return New EMoney(Math.Round((OrderPrice.Amount) * ((TaxClass.TaxRate + 100) / 100), 2, MidpointRounding.AwayFromZero), True, OrderPrice.Currency)
            End If
        End Get
    End Property

    Public Shared Function EmptyArticle(ByVal Amount As Decimal) As OrderedArticle
        Dim art As New OrderedArticle(New Article())
        art.ArticleID = -1
        art.ArticleDescription = "Keine Artikelbezeichnung verfügbar (Importierter Auftrag)"
        art.OrderPrice = New EMoney(Amount, True)
        art.OrderedAmount = 1
        Return art
    End Function

    Public Shared Function EmptyArticle() As OrderedArticle
        Dim art As New OrderedArticle(New Article())
        art.ArticleID = -1
        art.ArticleDescription = "Keine Artikelbezeichnung verfügbar (Importierter Auftrag)"
        art.OrderedAmount = 1
        Return art
    End Function
End Class

Public Class OrderedArticleSorter
    Implements System.Collections.Generic.IComparer(Of FuncClasses.OrderedArticle)

    Public Function Compare(x As OrderedArticle, y As OrderedArticle) As Integer Implements IComparer(Of OrderedArticle).Compare
        Return x.OrderPosition - y.OrderPosition
    End Function
End Class